{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 损失函数立体化呈现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(569, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(569,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "## 准备数据\n",
    "X,y = datasets.load_breast_cancer(return_X_y=True)\n",
    "X = X[:,:2]\n",
    "display(X.shape, y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>LogisticRegression()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">LogisticRegression</label><div class=\"sk-toggleable__content\"><pre>LogisticRegression()</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "LogisticRegression()"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 建模\n",
    "\n",
    "model = LogisticRegression()\n",
    "model.fit(X,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方程系数: -1.0462619024003452 -0.21688595452490866 [19.67135103]\n"
     ]
    }
   ],
   "source": [
    "## 逻辑回归中线性方程\n",
    "\n",
    "w1 = model.coef_[0,0]\n",
    "w2 = model.coef_[0,1]\n",
    "b = model.intercept_\n",
    "print(\"方程系数:\",w1,w2,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## sigmoid函数\n",
    "\n",
    "def sigmoid(x,w1,w2,b):\n",
    "    z = w1*X[0]+w2*X[1]+b # 线性方程\n",
    "    return 1/(1+np.exp(-z)) # sigmoid函数\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 定义损失函数\n",
    "\n",
    "def loss_function(X,y,w1,w2,b):\n",
    "    loss = 0\n",
    "    for x_i,y_i in zip(X,y):\n",
    "        p = sigmoid(x_i ,w1,w2,b) # 概率\n",
    "        loss +=-(y_i*np.log(p)+(1-y_i)*np.log(1-p)) # 损失\n",
    "    return loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-3.0462619 , -3.00585786, -2.96545382, -2.92504978, -2.88464574,\n",
       "       -2.8442417 , -2.80383766, -2.76343362, -2.72302958, -2.68262554,\n",
       "       -2.6422215 , -2.60181746, -2.56141342, -2.52100938, -2.48060534,\n",
       "       -2.4402013 , -2.39979726, -2.35939322, -2.31898918, -2.27858513,\n",
       "       -2.23818109, -2.19777705, -2.15737301, -2.11696897, -2.07656493,\n",
       "       -2.03616089, -1.99575685, -1.95535281, -1.91494877, -1.87454473,\n",
       "       -1.83414069, -1.79373665, -1.75333261, -1.71292857, -1.67252453,\n",
       "       -1.63212049, -1.59171645, -1.55131241, -1.51090837, -1.47050433,\n",
       "       -1.43010029, -1.38969625, -1.34929221, -1.30888817, -1.26848412,\n",
       "       -1.22808008, -1.18767604, -1.147272  , -1.10686796, -1.06646392,\n",
       "       -1.02605988, -0.98565584, -0.9452518 , -0.90484776, -0.86444372,\n",
       "       -0.82403968, -0.78363564, -0.7432316 , -0.70282756, -0.66242352,\n",
       "       -0.62201948, -0.58161544, -0.5412114 , -0.50080736, -0.46040332,\n",
       "       -0.41999928, -0.37959524, -0.3391912 , -0.29878715, -0.25838311,\n",
       "       -0.21797907, -0.17757503, -0.13717099, -0.09676695, -0.05636291,\n",
       "       -0.01595887,  0.02444517,  0.06484921,  0.10525325,  0.14565729,\n",
       "        0.18606133,  0.22646537,  0.26686941,  0.30727345,  0.34767749,\n",
       "        0.38808153,  0.42848557,  0.46888961,  0.50929365,  0.54969769,\n",
       "        0.59010173,  0.63050577,  0.67090981,  0.71131386,  0.7517179 ,\n",
       "        0.79212194,  0.83252598,  0.87293002,  0.91333406,  0.9537381 ])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-2.21688595, -2.17648191, -2.13607787, -2.09567383, -2.05526979,\n",
       "       -2.01486575, -1.97446171, -1.93405767, -1.89365363, -1.85324959,\n",
       "       -1.81284555, -1.77244151, -1.73203747, -1.69163343, -1.65122939,\n",
       "       -1.61082535, -1.57042131, -1.53001727, -1.48961323, -1.44920919,\n",
       "       -1.40880515, -1.36840111, -1.32799707, -1.28759303, -1.24718898,\n",
       "       -1.20678494, -1.1663809 , -1.12597686, -1.08557282, -1.04516878,\n",
       "       -1.00476474, -0.9643607 , -0.92395666, -0.88355262, -0.84314858,\n",
       "       -0.80274454, -0.7623405 , -0.72193646, -0.68153242, -0.64112838,\n",
       "       -0.60072434, -0.5603203 , -0.51991626, -0.47951222, -0.43910818,\n",
       "       -0.39870414, -0.3583001 , -0.31789606, -0.27749202, -0.23708797,\n",
       "       -0.19668393, -0.15627989, -0.11587585, -0.07547181, -0.03506777,\n",
       "        0.00533627,  0.04574031,  0.08614435,  0.12654839,  0.16695243,\n",
       "        0.20735647,  0.24776051,  0.28816455,  0.32856859,  0.36897263,\n",
       "        0.40937667,  0.44978071,  0.49018475,  0.53058879,  0.57099283,\n",
       "        0.61139687,  0.65180091,  0.69220495,  0.73260899,  0.77301304,\n",
       "        0.81341708,  0.85382112,  0.89422516,  0.9346292 ,  0.97503324,\n",
       "        1.01543728,  1.05584132,  1.09624536,  1.1366494 ,  1.17705344,\n",
       "        1.21745748,  1.25786152,  1.29826556,  1.3386696 ,  1.37907364,\n",
       "        1.41947768,  1.45988172,  1.50028576,  1.5406898 ,  1.58109384,\n",
       "        1.62149788,  1.66190192,  1.70230596,  1.74271001,  1.78311405])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "w1_space = np.linspace(w1-2, w1+2, 100)\n",
    "w2_space = np.linspace(w2-2, w2+2, 100)\n",
    "display(w1_space, w2_space)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/7b/yqxklffs4f34gglz8nv4w9s00000gn/T/ipykernel_3238/2409084974.py:7: RuntimeWarning: invalid value encountered in log\n",
      "  loss +=-(y_i*np.log(p)+(1-y_i)*np.log(1-p)) # 损失\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan, 747.89819343],\n",
       "       [         nan, 382.11020593],\n",
       "       [         nan, 774.35380395],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [443.51082264,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan],\n",
       "       [         nan,          nan]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 损失计算\n",
    "\n",
    "loss1_ = np.array([loss_function(X,y,i,w2,b) for i in w1_space])\n",
    "loss1_"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
